<?php
/**
 * Application level Controller
 *
 * This file is application-wide controller file. You can put all
 * application-wide controller-related methods here.
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @package       app.Controller
 * @since         CakePHP(tm) v 0.2.9
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
 */
App::uses('Controller', 'Controller');
App::uses('Folder', 'Utility');
App::uses('File', 'Utility');

/**
 * Application Controller
 *
 * Add your application-wide methods in the class below, your controllers
 * will inherit them.
 *
 * @package		app.Controller
 * @link		http://book.cakephp.org/2.0/en/controllers.html#the-app-controller
 */

App::Import('Model', 'ToplantiTutanagi');

class AdminsController extends AppController {
	public $helpers = array('Html', 'Form');
	public $components = array('Paginator');
	public $paginate = array(
		'limit' => 5,
		'order' => array(
			'Egitmen.ad' => 'asc'
		)
	);
	
	public function listToplantiTutanagi () {
		$this->ToplantiTutanagi = new ToplantiTutanagi();
		$this->layout = null ;
		$this->paginate['ToplantiTutanagi'] = array(
			'conditions' => array('ToplantiTutanagi.konu LIKE' => $this->request->query('konu') . '%'),
			'limit' => $this->request->query('limit'),
			'page' => $this->request->query('page')
		);
		
		if ($this->request->query('sort')) {
			$sortArr = json_decode($this->request->query['sort'], true);
			if (count($sortArr) == 1) {
				$property =  $sortArr[0]['property'];
				$direction =  $sortArr[0]['direction'];
				$this->paginate['ToplantiTutanagi']['order'] =  array(
					'ToplantiTutanagi.' . $property => $direction
				);
			}			
		}

		// similar to findAll(), but fetches paged results
		$data = $this->paginate('ToplantiTutanagi');
		//$this->Paginator->counter();//$this->Paginator->paging['count']; //$this->Paginator->find('count');
		//echo $count . "xxxx";
		$count = $this->params['paging']['ToplantiTutanagi']['count'];
		//debug($data, true);
		$this->set('tts', $data);
		$this->set('count', $count);
	}
	
	public function listEgitmenEgitim () {
		$id = $this->request->query('id');
		if (!$id) {
			$id = $this->request->data->id;
		}
		$this->autoRender = false;
		$this->layout = 'ajax';
		$data = $this->Egitmen->read(null, $id);
		$egitimler = $data['Egitimler'];
		
		$resultEgitimler = array();
		foreach ($egitimler as $egitim) {
			$node = new N();
			$node->id = $egitim['id'];
			$node->baslik = $egitim['baslik'];
			$node->src = 'egitimapp/resources/images/egitim_big.png';
			array_push($resultEgitimler, $node);
		}
		echo "{results:" . json_encode($resultEgitimler) . "}";
		
	}
	
	/**
	 * Eğitime dahil olmuş eğitmenleri döner
	 * @param string $egitimId
	 */
	public function listEgitimEgitmen () {
		$this->autoRender = false;
		$this->layout = 'ajax';

		$egitimId = $this->request->query('egitimId');
		if (!$egitimId) {
			$egitimId = $this->request->data->egitimId;
		}
		
		$this->Egitmen->bindModel(array('hasOne' => array('EgitimsEgitmen')));
		
		//FIXME daha kısıtlı bilgi al
		$data = $this->Egitmen->find('all', array(
				'fields' => array('id', 'ad', 'soyad'),
				'conditions' => array('EgitimsEgitmen.egitim_id' => $egitimId),
				'contain' => array()
				//'recursive' => 2
		));
		
		$resultEgitmenler = array();
		foreach ($data as $egitmen) {
			$egt = $egitmen['Egitmen'];
			$node = new N1();
			$node->id = $egt['id'];
			$node->adSoyad = $egt['ad'] . ' ' . $egt['soyad'];
			$node->src = 'egitimapp/resources/images/egitmen_big.png';
			array_push($resultEgitmenler, $node);
		}
		echo "{results:" . json_encode($resultEgitmenler) . "}";
		
	}
	
	public function saveToplantiTutanagi() {
		//$this->layout = null ;
		$this->autoRender = false;
		$this->layout = 'ajax';
		//CakeLog::write('debug',"data " . $this->data);
		$media = ClassRegistry::init('Media');
		$toplantiTutanagi = ClassRegistry::init('ToplantiTutanagi');
		//debug($media, true);
		if ($this->request->is('post')) {
			$toplantiTutanagi->create();
			$result = FALSE;
			$dokuman_media_id = null;
			$tempFilePath = $_FILES['dokumandata']['tmp_name'];
			
			//CakeLog::write('debug', 'filename: '. $tempFilePath);
			//CakeLog::write('debug', 'filename: '. $_FILES['cvdata']['name']);
			
			$realFileName = $_FILES['dokumandata']['name'];
			//$filename = "app/webroot/files/".$realFileName;
			/* copy uploaded file */
			//if (move_uploaded_file($_FILES['dokumandata']['tmp_name'], $filename)) {
				//CakeLog::write('debug', 'filesize: '. filesize($filename));
				$fileSize = filesize($tempFilePath);
				$fileData = fread(fopen($tempFilePath, "r"), $fileSize);
				
				//Debugger::log("REAL FILE NAME", end(explode(".", $realFileName)));
				
				$media->save(array(
						name => $realFileName,
						ext => end(explode(".", $realFileName)),
						content => $fileData,
						size => $fileSize,
						type => end(explode(".", $realFileName))
				));
				$dokuman_media_id = $media->id;
			//}
			
			$data = $this->request->data;
			$data['dokuman_media_id'] = $dokuman_media_id;
			//Debugger::log($data);
			$result = $toplantiTutanagi->save($data);
			//Debugger::log($toplantiTutanagi);
			
			if ($result) {
				echo "{msg:'Toplantı Tutanağı Başarıyla Kaydedildi', success: true}";
			} else {
				echo "{msg:'hatalı', success: false}";
			}
		}
	}
	
	public function delete($id = null) {
		//$this->layout = null ;
		$this->autoRender = false;
		$this->layout = 'ajax';
		
		$this->Egitmen->id = $id;
		
		if ($this->Egitmen->delete()) {
			echo "{msg:'Silme Başarıyla Tamamlandı', success: true}";
		} else {
			echo "{msg:'hatalı', success: false}";
		}
	
	}
	
	public function load($id = null) {
		$this->layout='ajax' ;
		$this->Egitmen->id = $id;
		if (!$this->Egitmen->exists()) {
			//throw new NotFoundException(__('Invalid user'));
		}
		$this->set('egitmen', $this->Egitmen->read(null, $id));
	}
	
	public function downloadCV($id = null) {
		$this->autoRender = false;
		$this->layout = 'ajax';
		
		$this->Egitmen->id = $id;
		if (!$this->Egitmen->exists()) {
			//throw new NotFoundException(__('Invalid user'));
		}
		$egitmen = $this->Egitmen->read(null, $id);
		//debug($egitmen, true);
		$cvMediaId = $egitmen['Egitmen']['cv_media_id'];
		$this->viewClass = 'Media';
		$media = ClassRegistry::init('Media');
		$media->id = $cvMediaId;
		
		$mediaData = $media->read()['Media'];
		
		header('Content-type: ' . 'image/jpeg');
		header('Content-length: ' . $mediaData['size']);
		header('Content-Disposition: attachment; filename='. 'CV.jpg');
		
		
		//debug($media, true);
		echo  $media->read()['Media']['content'];
		exit();
		//echo $cvMediaId;
	}
	
	public function saveEgitim () {
		$this->autoRender = false;
		$this->layout = 'ajax';
		
		$egitimsEgitmen = ClassRegistry::init('EgitimsEgitmen');
		$dataSource = $egitimsEgitmen->getDataSource();
		$egitmenId = $this->request->query('egitmenId');
		$egitimIds = split(",", $this->request->query('egitimIds'));
		
		//debug($egitimIds, true);
		
		$result = $egitimsEgitmen->deleteAll(array('egitmen_id' => $egitmenId), false);
		if ($result) {
			if (count($egitimIds) > 0) {
				foreach ($egitimIds as $egitimId) {
					if ($egitimId != NULL) {
						$egitimsEgitmen->create();
						$egitimsEgitmen->save(array(
								'egitmen_id' => $egitmenId,
								'egitim_id' => $egitimId
						));
					}
				}
			}			
		}
		
		if ($result) {
			$dataSource->commit();
		} else {
			$dataSource->rollback();
		}
		
		if ($result) {
			echo "{msg:'Başarıyla Tamamlandı', success: true}";
		} else {
			echo "{msg:'hatalı', success: false}";
		}
		
	}

}
